﻿@page "/admin/settings/advanced"
@using System.Reflection
@using Microsoft.AspNetCore.Mvc.TagHelpers
@Html.AntiForgeryToken()
@{
    var settings = BlogConfig.AdvancedSettings;

    var asm = Assembly.GetEntryAssembly();
    var currentVersion = (new Version(asm!.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version)).ToString();
}
@section scripts{
    <script type="module">
        import * as settings from '/js/app/admin.settings.module.js';

        function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            setInterval(function () {
                minutes = parseInt(timer / 60, 10);
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? '0' + minutes : minutes;
                seconds = seconds < 10 ? '0' + seconds : seconds;

                display.text(minutes + ':' + seconds);

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
        }

        $('.btn-restart').click(function () {
            callApi(`/api/settings/shutdown`, 'POST', {}, () => { });
            $('.btn-restart').text('Wait...');
            $('.btn-restart').addClass('disabled');
            $('.btn-restart').attr('disabled', 'disabled');
            startTimer(30, $('.btn-restart'));
            setTimeout(function () {
                location.href = '/admin/settings';
            }, 30000);
        });

        $('.btn-reset').click(function () {
            callApi(`/api/settings/reset`, 'POST', {}, () => { });
            $('.btn-reset').text('Wait...');
            $('.btn-reset').addClass('disabled');
            $('.btn-reset').attr('disabled', 'disabled');
            startTimer(30, $('.btn-reset'));
            setTimeout(function () {
                location.href = '/';
            }, 30000);
        });

        $('.btn-check-update').click(function () {
            $('.spinner-border').show();
            $('.alert-has-new-release').hide();
            $('.alert-no-new-release').hide();
            $('.btn-get-update').addClass('disabled');
            $('.btn-get-update').attr('href', '#');

            var updateCheckCanvas = new bootstrap.Offcanvas(document.getElementById('updateCheckCanvas'));
            updateCheckCanvas.show();

            fetch('https://api.github.com/repos/EdiWang/Moonglade/releases/latest', {
                method: 'GET',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                }
            }).then(async (resp) => {
                var data = await resp.json();
                $('.spinner-border').hide();

                $('#currentAssemblyFileVersion').val('@currentVersion');
                $('#releaseName').val(data.name);
                $('#releaseTagName').val(data.tag_name);
                $('#releaseCreatedAt').val(new Date(data.created_at));

                var c = settings.compareVersionNumbers(data.tag_name.replace('v', ''), '@currentVersion');
                var hasNewVersion = c == 1 && !data.prerelease;

                if (hasNewVersion) {
                    $('.alert-has-new-release').show();
                    $('.btn-get-update').removeClass('disabled');
                    $('.btn-get-update').attr('href', data.html_url);
                }
                else {
                    $('.alert-no-new-release').show();
                }
            }).catch(err => {
                blogToast.error(err);
                console.error(err);
            });
        });

        window.generateMetaWeblogPassword = function() {
            callApi('/api/settings/password/generate', 'GET', {}, async (resp) => {
                var data = await resp.json();
                $('#settings_MetaWeblogPassword').val(data.password);
            });
        }

        const form = document.querySelector('#form-settings');
        form.addEventListener('submit', settings.handleSettingsSubmit);
    </script>
}

<partial name="_SettingsHeader" />
<div class="ps-4 pe-4">
    <form id="form-settings" asp-controller="Settings" asp-action="Advanced">
        <div class="row g-5">
            <div class="col-md-6">
                <div class="mb-4">
                    <h6 class="card-subtitle mb-4 text-muted">@SharedLocalizer["Advanced Settings"]</h6>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-arrow-right-circle settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnablePingback" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnablePingback" value="false">
                                <input type="checkbox" name="EnablePingback" value="true" class="form-check-input" @(settings.EnablePingback ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-map settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableSiteMap" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnableSiteMap" value="false">
                                <input type="checkbox" name="EnableSiteMap" value="true" class="form-check-input" @(settings.EnableSiteMap ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-search settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableOpenSearch" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnableOpenSearch" value="false">
                                <input type="checkbox" name="EnableOpenSearch" value="true" class="form-check-input" @(settings.EnableOpenSearch ? "checked" : null)>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-diagram-2 settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableFoaf" class="form-check-label"></label>
                            <div class="form-text">@SharedLocalizer["* Requires restarting application"]</div>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnableFoaf" value="false">
                                <input type="checkbox" name="EnableFoaf" value="true" class="form-check-input" @(settings.EnableFoaf ? "checked" : null)>
                            </div>
                        </div>
                    </div>
                    
                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-filetype-xml settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableOpml" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnableOpml" value="false">
                                <input type="checkbox" name="EnableOpml" value="true" class="form-check-input" @(settings.EnableOpml ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-exclamation-triangle settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.WarnExternalLink" class="form-check-label"></label>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="WarnExternalLink" value="false">
                                <input type="checkbox" name="WarnExternalLink" value="true" class="form-check-input" @(settings.WarnExternalLink ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="mt-5 mb-2">
                        <h6 class="card-subtitle mb-3 text-muted">@Html.DisplayNameFor(m => settings.RobotsTxtContent)</h6>
                        <textarea asp-for="@settings.RobotsTxtContent" class="form-control textarea-code" cols="60" rows="10" spellcheck="false"></textarea>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="mb-5">
                    <h6 class="card-subtitle mb-4 text-muted">
                        MetaWeblog
                    </h6>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-puzzle settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            <label asp-for="@settings.EnableMetaWeblog" class="form-check-label"></label>
                            <div class="form-text">
                                @SharedLocalizer["* Requires restarting application"]
                            </div>
                        </div>
                        <div class="col-md-4 text-end">
                            <div class="form-check form-switch">
                                <input type="hidden" name="EnableMetaWeblog" value="false">
                                <input type="checkbox" name="EnableMetaWeblog" value="true" class="form-check-input" @(settings.EnableMetaWeblog ? "checked" : null)>
                            </div>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-key settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["Password"]
                            <div class="form-text">
                                @SharedLocalizer["Leave blank if you don't need to change password."]
                                @SharedLocalizer["* Username: moonglade"]
                            </div>
                        </div>
                        <div class="col-md-4">
                            <input asp-for="@settings.MetaWeblogPassword" class="form-control" minlength="8" maxlength="16" />

                            <a href="javascript:;" class="btn btn-sm btn-outline-accent mt-2" onclick="generateMetaWeblogPassword()">@SharedLocalizer["Generate password"]</a>
                        </div>
                    </div>
                </div>
                <div>
                    <h6 class="card-subtitle mb-4 text-muted">
                        @SharedLocalizer["Options"]
                    </h6>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-moon-stars settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["Update"]
                            <div class="form-text">
                                @SharedLocalizer["Check online for new Moonglade release."]
                            </div>
                        </div>
                        <div class="col-md-5 text-end">
                            <a href="#" class="btn btn-sm btn-outline-accent btn-check-update">
                                @SharedLocalizer["Check now"]
                            </a>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-power settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["Restart Website"]
                            <div class="form-text">
                                @SharedLocalizer["Try to shutdown and restart the website, this will terminate all current requests."]
                            </div>
                        </div>
                        <div class="col-md-5 text-end">
                            <a href="javascript:;" class="btn btn-sm btn-outline-accent" data-bs-toggle="modal" data-bs-target="#restartModal">
                                @SharedLocalizer["Restart"]
                            </a>
                        </div>
                    </div>

                    <div class="row g-3 align-items-center settings-entry mb-4">
                        <div class="col-auto">
                            <i class="bi-eraser settings-entry-icon"></i>
                        </div>
                        <div class="col">
                            @SharedLocalizer["System Reset"]
                            <div class="form-text">
                                @SharedLocalizer["Erase all data and configuration."]
                            </div>
                        </div>
                        <div class="col-md-5 text-end">
                            <a href="javascript:;" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#resetModal">
                                @SharedLocalizer["Reset"]
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mb-4">
            <input id="btn-save-settings" type="submit" value="@SharedLocalizer["Save"]" class="btn btn-outline-accent" />
        </div>
    </form>
</div>
<div class="modal fade" id="restartModal" tabindex="-1" role="dialog" aria-labelledby="restartModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="restartModalLabel">@SharedLocalizer["Restart Website"]</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                </button>
            </div>
            <div class="modal-body">
                @SharedLocalizer["Are you sure to restart website? All current requests will be terminated."]
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-restart">@SharedLocalizer["Restart"]</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="resetModal" tabindex="-1" role="dialog" aria-labelledby="resetModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="resetModalLabel">@SharedLocalizer["System Reset"]</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                </button>
            </div>
            <div class="modal-body">
                @SharedLocalizer["Are you ABSOLUTELY sure? ALL data and configuration will be erased!"]
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-reset">@SharedLocalizer["Reset"]</button>
            </div>
        </div>
    </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="updateCheckCanvas" aria-labelledby="updateCheckCanvasLabel">
    <div class="offcanvas-header">
        <h5 class="offcanvas-title" id="updateCheckCanvasLabel">@SharedLocalizer["Moonglade Update"]</h5>
        <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <div class="spinner-border" role="status" style="display: none;">
            <span class="visually-hidden">Loading...</span>
        </div>

        <div class="alert alert-info alert-no-new-release">
            <i class="bi-info-circle"></i>
            You are running the latest version already.
        </div>

        <div class="alert alert-success alert-has-new-release">
            <i class="bi-lightbulb"></i>
            There is a new version of Moonglade available!
        </div>

        <div class="release-info">
            <h6>
                <i class="bi-box-seam"></i>
                Latest Release
            </h6>
            <hr />
            <div class="mb-1 row">
                <label for="releaseName" class="col-4 col-form-label">
                    <strong>@SharedLocalizer["Name"]</strong>
                </label>
                <div class="col-8">
                    <input type="text" readonly class="form-control-plaintext" id="releaseName" value="...">
                </div>
            </div>
            <div class="mb-1 row">
                <label for="releaseTagName" class="col-4 col-form-label">
                    <strong>@SharedLocalizer["Tag name"]</strong>
                </label>
                <div class="col-8">
                    <input type="text" readonly class="form-control-plaintext" id="releaseTagName" value="...">
                </div>
            </div>
            <div class="mb-1 row">
                <label for="releaseCreatedAt" class="col-4 col-form-label">
                    <strong>@SharedLocalizer["Release time"]</strong>
                </label>
                <div class="col-8">
                    <input type="text" readonly class="form-control-plaintext" id="releaseCreatedAt" value="...">
                </div>
            </div>
        </div>
        <hr />
        <div class="mt-3">
            <a href="#" target="_blank" class="btn btn-outline-accent disabled btn-get-update">
                <i class="bi-download"></i>
                @SharedLocalizer["Get update"]
            </a>
            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="offcanvas">@SharedLocalizer["Cancel"]</button>
        </div>
    </div>
</div>
